package com.stropheware.pathfinding;

import com.stropheware.droidDefense.AbstractEnemy;
import com.stropheware.droidDefense.Cell;
import com.stropheware.droidDefense.GameBoard;

import android.util.Log;


public class Node implements Comparable <Node> {

	public int cost;
	public int depth;
	public float heuristic;
	
	public Node parent;
	
	public int indexX,indexY;
	
	public boolean visited;
	
	public Cell[] occupiedCells;
	
	public int xPx;
	public int yPx;
	
	public int groupNum = - 1;
	
	public Node(int x, int y, GameBoard board){
		indexX = x;
		indexY = y;
		visited = false;
		
		/**This needs to be changed for SIZExSIZE, can we make it dynamic?*/
		occupiedCells = new Cell[AbstractEnemy.SIZE*AbstractEnemy.SIZE];
		occupiedCells[0]=board.cellGrid[x][y];//topleft
		occupiedCells[1]=board.cellGrid[x+1][y];//topmiddle
		occupiedCells[2]=board.cellGrid[x+2][y];//topright
		occupiedCells[3]=board.cellGrid[x][y+1];//leftmiddle
		occupiedCells[4]=board.cellGrid[x+1][y+1];//middle
		occupiedCells[5]=board.cellGrid[x+2][y+1];//rightmiddle
		occupiedCells[6]=board.cellGrid[x][y+2];//bottomleft
		occupiedCells[7]=board.cellGrid[x+1][y+2];//bottommiddle
		occupiedCells[8]=board.cellGrid[x+2][y+2];//bottomright
		xPx = occupiedCells[0].pxColumn;
		yPx = occupiedCells[0].pxRow;
	}
	
	public boolean equals(Object obj){
		if(obj instanceof Node && this.indexX == ((Node)obj).indexX && this.indexY == ((Node)obj).indexY)
			return true;
		else
			return false;
	}

	public int compareTo(Node another) {
		float f = heuristic + cost;
		float of = another.heuristic + another.cost;
		
		if (f < of) {
			return -1;
		} else if (f > of) {
			return 1;
		} else {
			return 0;
		}
	}
	
	public int setParent(Node parent) {
		depth = parent.depth + 1;
		this.parent = parent;
		return depth;
	}
	
	public boolean containsTower(){
		for(int i = 0; i < this.occupiedCells.length; i++){
			if(this.occupiedCells[i].content == Cell.TOWER) return true;
		}
		return false;
	}

}
